<?php

include 'format.php';

FormatHeader();
function Mno_user(&$username){
	echo '<h1> Username does not exist </h1>';
	echo_log();
	$username=false;
	return false;
}
$WPOJ_Is_Logged = 0;
if ($_COOKIE["user"] && $_COOKIE["password"]){
	mysql_connect("localhost", "root", "apache") or die(mysql_error());
	mysql_select_db("jobs") or die(mysql_error());
	$username=true;
	//echo $_COOKIE["user"].$_COOKIE["password"];
	//echo "SELECT * FROM users where user = '".$_COOKIE["user"]."'";
	$result = mysql_query("SELECT * FROM users where user = '".$_COOKIE["user"]."'") or die(mysql_error()); 
	$row = mysql_fetch_array( $result ) or Mno_user($username);
	if ($username == true){
		if ($_COOKIE["password"]){
			$encryptpass = $_COOKIE["password"];
			if ($encryptpass == $row['password']){
				echo '<a href="/logout.php">Log Out</a><br>';
				echo $row['user'].'<br><hr>';
				$WPOJ_Is_Logged = 1;
				$WPOJ_User = $row['user'];
				$WPOJ_Rank = $row['rights'];
				if ($row['rights'] > 0){
					if ($row['rights'] == 2){
						// pun ce apare la admini
						# forum admin
						echo '<a href="/reeval.php">reevaluate submissions</a><br>';
						echo '<a href="/forum/adm">forum admin</a><br>';
						echo '<a href="/addcontest.php">add contest</a><br><hr>';
					}
					echo '<a href="/addproblem.php">add problem</a><br>';
					echo '<a href="/uploadfiles.php">upload files</a><br>';
					echo '<a href="/myproblemlist.php">problem list</a><br><hr>';					
					// pun ce apare la admini + helperi
				}
				echo '<a href="/myaccount.php">my account</a><br>';
				// pun ce apare la toti
				echo '<br>';
			}
			else
				echo_unreg();
		}
		else
			echo_unreg();
	}
	else
		echo_unreg();
}
else
	echo_unreg();
echo '
	<a href="/status.php">status</a><br>
	<a href="/submit.php">submit</a><br>
	<a href="/problems.php">problems</a><br><br>
	<a href="/">news</a><br>
	<a href="/contests.php">contests</a><br>
	<a href="/ranks.php">ranks</a><br><br>
	<a href="/forum">forum</a><br>
	<a href="/help.php">help</a><br><hr></div><div class="time">
Server time:
</div>
<div class="time">';
echo date("Y-m-d");
echo '<br><b>';
echo date("H : m : s");
echo '</b>
</div>
</td><td class="content" width="99%">
<p align="justify">';

echo'
	<body  bgcolor=#FFFFFF style = \'font-size : 9pt;\' text=#FFFFFF link=#336699 vlink=#336699 alink=#336699><div align="center">
		<TABLE BORDER="5"    WIDTH="100%"   CELLPADDING="4" CELLSPACING="3">
	   <TR ALIGN="CENTER">
            <TH>JOB ID</TH>
			<TH>DATE </TH>
			<TH>USER </TH>
			<TH>TASK </TH>
			<TH>LANG </TH>
			<TH>SIZE </TH>
			<TH>STATUS</TH>
	 </TR>';
 
 $String = "Select * from job "; $Ok = 0;

	$WPOJ_FILTER_USER = $_GET['user']; 
	if($WPOJ_FILTER_USER){
		$Ok = 1;
		$String2 = $String2."?user=".$WPOJ_FILTER_USER;
		$String = $String."where user = '".$WPOJ_FILTER_USER."' ";
	}
	$WPOJ_FILTER_TASK = $_GET['task'];	
	if ($WPOJ_FILTER_TASK){
		if ($Ok == 0){
			$Ok = 1;
			$String = $String."where problem = '".$WPOJ_FILTER_TASK."' ";
			$String2 = $String2."?task=".$WPOJ_FILTER_TASK;
		}
		else{
			$String = $String."and problem = '".$WPOJ_FILTER_TASK."' ";
			$String2 = $String2."&task=".$WPOJ_FILTER_TASK;
		}
	}	
	$WPOJ_FILTER_CONTEST = $_GET['contest'];
	if ($WPOJ_FILTER_CONTEST){
		if ($Ok == 0){
			$Ok = 1;
			$String2 = $String2."?contest=".$WPOJ_FILTER_CONTEST;
			$String = $String."where contest = '".$WPOJ_FILTER_CONTEST."' ";
		}
		else{
			$String2 = $String2."&contest=".$WPOJ_FILTER_CONTEST;
			$String = $String."and contest = '".$WPOJ_FILTER_CONTEST."' ";
		}
	}
	
	$WPOJ_FILTER_Score_begin = $_GET['score_begin'];
	if ($WPOJ_FILTER_Score_begin){
		if ($Ok == 0){
			$Ok = 1;
			$String2 = $String2."?score_begin=".$WPOJ_FILTER_Score_begin;
			$String = $String."where Points >= ".$WPOJ_FILTER_Score_begin." ";
		}
		else{
			$String2 = $String2."&score_begin=".$WPOJ_FILTER_Score_begin;
			$String = $String."and Points >= ".$WPOJ_FILTER_Score_begin." ";
		}
	}
	
	$WPOJ_FILTER_Score_end = $_GET['score_end']; 
	if ($WPOJ_FILTER_Score_end){
		if ($Ok == 0){
			$Ok = 1;
			$String2 = $String2."?score_end=".$WPOJ_FILTER_Score_end;
			$String = $String."where Points <= ".$WPOJ_FILTER_Score_end." ";
		}
		else{
			$String2 = $String2."&score_end=".$WPOJ_FILTER_Score_end;
			$String = $String."and Points <= ".$WPOJ_FILTER_Score_end." ";
		}
	}
	
	$WPOJ_SUBMITS_PER_PAGE = 20;
	$WPOJ_SUBMITS = $_GET['show_submits'];
	if ($WPOJ_SUBMITS)
		$WPOJ_SUBMITS_PER_PAGE = $WPOJ_SUBMITS;
	$String = $String." order by Id desc";
	
 mysql_connect("localhost", "root", "apache") or die(mysql_error());
 mysql_select_db("jobs") or die(mysql_error());
 $result = mysql_query($String) or die(mysql_error());  
 $i = 0;
 while ( ($row = mysql_fetch_array( $result )) && $i < $WPOJ_SUBMITS_PER_PAGE ){
 	++$i;
  	//$result = mysql_query("select * from job where id=".$i) or die(mysql_error());  
 	//$row = mysql_fetch_array( $result ) or die(mysql_error());
 	///echo "i=",$row['Id']."|";
	echo'<TR ALIGN="CENTER">';
	echo'<TD>'.$row['Id'].'</TD>';
	echo'<TD>'.$row['Date'].'</TD>';
	echo'<TD><A href="/user/'.$row['User'].'.php">'.$row['User'].'</A></TD>';
	echo'<TD><A href="/task/',$row['Problem'].'.php">'.$row['Problem'].'</A></TD>';
	echo'<TD>'.$row['Compiler'].'</TD>'; 
	if ($WPOJ_Is_Logged = 1 && ($WPOJ_User == $row['User'] || $WPOJ_Rank == 2)){
		echo'<TD><A href="viewsource.php?jobnr='.$row['Id'].'">'.$row['Size'].'kb</A></TD>';
		//echo '<TD>Ceva</TD>';
	}
	else{
		echo'<TD>'.$row['Size'].'kb</TD>';
	}
	if ($row['Status'] == 1)
		echo'<TD><A href="viewjob.php?jobnr='.$row['Id'].'">Finished :'.$row['Points'].' Points</A></TD>';
	else
		echo'<TD><A href="viewjob.php?jobnr='.$row['Id'].'">Waiting... </A></TD>';
    echo '</TR>';
    	//echo $row['Size']." ".$row['User']." ".$WPOJ_User." ".$WPOJ_Rank." ";
 }
 echo '</TABLE>';
 if ($WPOJ_Rank == 2)
 	echo '<A href="reeval.php'.$String2.'">Reevalueaza</A>';
 echo'
 </body>';
FormatFooter();
?>
